package com.gitee.ywj1352.算法.真实;

import com.gitee.ywj1352.算法.week02.TreeNode;

public class 二叉树最大距离 {
    public static int ans = Integer.MIN_VALUE;


    public static int maxPath(TreeNode root) {
        helper(root);
        return ans - 1;
    }

    //还是有问题
    public static int helper(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = helper(root.left);
        int right = helper(root.right);
        ans = Math.max(ans, left + right + 1);
        return Math.max(left,right)+ 1;
    }

    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        TreeNode t4 = new TreeNode(4);
        TreeNode t5 = new TreeNode(5);
        TreeNode t6 = new TreeNode(6);
        TreeNode t7 = new TreeNode(7);
        TreeNode t8 = new TreeNode(8);
        t5.left = t3;
        t5.right = t8;
        t3.left = t2;
        t3.right = t4;
        t2.left = t1;
        t8.left = t6;
        t8.right = t7;
        int i = maxPath(t5);
        System.out.println(i);
    }


}
